package cn.edu.hqu.cst.lzs.homework4;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    Button btnBindService,btnUnbindService,btnGetStatus;
    TextView tvServiceStatus;
    MyService.MyServiceBinder serviceBinder;
    boolean isServiceBind=false;
    ServiceConnection conn=new ServiceConnection() {
        private static final String TAG = "MainActivity";

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            serviceBinder = (MyService.MyServiceBinder) service;
            Log.d(TAG,"Service is connected");
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(TAG,"Service is disconnected");
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnBindService=findViewById(R.id.btn_main_activity_bind_service);
        btnUnbindService=findViewById(R.id.btn_main_activity_unbind_service);
        btnGetStatus=findViewById(R.id.btn_main_activity_get_status);
        tvServiceStatus=findViewById(R.id.tv_main_activity_service_status);
        btnBindService.setOnClickListener(this);
        btnUnbindService.setOnClickListener(this);
        btnGetStatus.setOnClickListener(this);
    }
    @SuppressLint({"NonConstantResourceId", "SetTextI18n"})
    @Override
    public void onClick(View v) {
        Intent intent=new Intent();
        intent.setClass(MainActivity.this,MyService.class);
        switch (v.getId()){
            case R.id.btn_main_activity_bind_service:
                if (!isServiceRunning()) {
                    intent.setClass(this,MyService.class);
                    bindService(intent,conn, Service.BIND_AUTO_CREATE);
                    tvServiceStatus.setText("绑定 Service");

                }
                else {
                    tvServiceStatus.setText("Service 已绑定");
                }
                break;
            case R.id.btn_main_activity_get_status:
                if (isServiceRunning()) {
                    tvServiceStatus.setText("Service 的 Count 值为"+ serviceBinder.getCount());
                }
                else {
                    tvServiceStatus.setText("Service 未绑定");
                }
                break;
            case R.id.btn_main_activity_unbind_service:
                if (isServiceRunning()) {
                    intent.setClass(this,MyService.class);
                    tvServiceStatus.setText("解绑 Service");
                    unbindService(conn);
                }
                else {
                    tvServiceStatus.setText("Service 未绑定");
                }
                break;
        }
    }
    private boolean isServiceRunning() {
        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if ("cn.edu.hqu.cst.lzs.homework4.MyService".equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }
}